package com.book.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.book.dao.BookDao;
import com.book.domain.Book;
import com.book.domain.SplitPage;

/**
 * Servlet implementation class TestSplitPageServlet
 */
public class TestSplitPageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public TestSplitPageServlet() {
		super();

	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String currentpage = request.getParameter("currentpage");
		if (currentpage == null) {
			currentpage = "1";
		}
		Book book = new Book();
		BookDao bookDao = new BookDao();
		int totalrecord = bookDao.totalRecord(book);
		int currentpagenumber = Integer.parseInt(currentpage);
		SplitPage splitPageold = new SplitPage();
		splitPageold.setCurrentpagenumber(currentpagenumber);
		splitPageold.setTotalrecord(totalrecord);
		splitPageold.setPagerecordnumber(1);
		SplitPage splitPagenew = SplitPage.calculatepage(splitPageold);

		List<Book> booksList = bookDao.splitpageBooks(splitPagenew);
		request.setAttribute("booksList", booksList);
		request.setAttribute("splitPagenew", splitPagenew);
		System.out.println(splitPagenew);
		request.getRequestDispatcher("/page/test/splitpage.jsp").forward(request, response);
		

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
